#include "BottomInteractable7.h"
#include <QDebug>

namespace TDigitalSignage
{
	/**
	Constructor
	*/
	BottomInteractable7::BottomInteractable7() :
		Interactable(),
		mGUIManager(),
		mApplicationDirectory()
	{
		setScale(0.00002f);
		
	}

	/**
	Destructor
	*/
	BottomInteractable7::~BottomInteractable7()
	{
	}

	/**
	* Loads the graphics to be used by interactable
	* \param inApplicationDir Path to the Applications data folder
	*/
	void BottomInteractable7::load(const QString& inApplicationDir)
	{
		mApplicationDirectory=inApplicationDir;
		// load the sprite
		mSprite.load(inApplicationDir+"ad_7.png");

		// check if the GUI resources has completed initialization. If yes then setup the object

		loadGUI(mApplicationDirectory);
		mGUIManager.addListener(this);

	}

	/**
	* Unloads the loaded graphics
	*/
	void BottomInteractable7::unload()
	{
		mSprite.unload();
		mGUIManager.unload();
	}

	/**
	Draw method
	*/
	void BottomInteractable7::draw()
	{
		
		// update the sprites properties
		mSprite.setPosition(getPosition());
		mSprite.setScale(getScale());
		//mSprite.setRotation(getRotation());
		mSprite.render();

		//For GUI
		repositionGUI();
		
		mGUIManager.render();
	
	}
	
	
	/**
	* onUpdate event handler
	* \param inFrameTime elapsed time in seconds since the last frame
	*/
	void BottomInteractable7::onUpdate(float inFrameTime)
	{
		// update the interactable parent class parameters
		Interactable::update(inFrameTime);

		// check if the video resources has completed initialization. If yes then setup the object
			
		loadGUI(mApplicationDirectory);

		//mSprite.update(inFrameTime);

		// Update the gui
		mGUIManager.update(inFrameTime);
			
	}

	/**
	* Loads the gui graphics resources
	* \param inAppDir Path to the application's data directory
	*/
	void BottomInteractable7::loadGUI( const QString& inAppDir )
	{
		
		// load mini three button
		mpMax = mGUIManager.addButton("MaxButton", inAppDir+"max.png", inAppDir+"max.png");
		mpCancel = mGUIManager.addButton("CancelButton", inAppDir+"cancel.png", inAppDir+"cancel.png");
		mpBigMax=mGUIManager.addButton("CancelBigButton", inAppDir+"cancelBig.png", inAppDir+"cancelBig.png");

	}


	/**
	* Finger down event handler

	* \param inData holds the touch information
	*/
	void BottomInteractable7::onFingerDown(const TouchListener::TouchData& inData)
	{
			
		// mGUIManager.onFingerUpdate(inData) returns true if the touch event was handled by the gui
		if(mGUIManager.onFingerDown(inData))
			return;
		Interactable::fingerDown(inData);

	}

	/**
	* Finger up event handler
	* \param inData holds the touch information
	*/
	void BottomInteractable7::onFingerUp(const TouchListener::TouchData& inData)
	{
		
		
		// mGUIManager.onFingerDown(inData) returns true if the touch event was handled by the gui
		if(mGUIManager.onFingerUp(inData))
			return;
		Interactable::fingerUp(inData);
		
	}

	/**
	* Finger update event handler
	* \param inData holds the touch information
	*/
	void BottomInteractable7::onFingerUpdate(const TouchListener::TouchData& inData)
	{
		

		// mGUIManager.onFingerUpdate(inData) returns true if the touch event was handled by the gui
		if(mGUIManager.onFingerUpdate(inData))
			return;

		// pass the touch event to update the parent's class parameters
		Interactable::fingerUpdate(inData);
		
	}


	
	// OnGUIEvent
	void BottomInteractable7::onGUIEvent(Blobz::GUIBase* inpComponent, Blobz::GUIEvent inGUIEvent, Blobz::FingerEvent inFingerEvent)
	{
		
		if(inGUIEvent == Blobz::Button_Released)
		{
			if(inpComponent->getName()=="CancelButton")
			{
				if(mSprite.isLoaded())
				{
				 
				/* One Way*/
				//mGUIManager.removeComponent("CancelButton");
				//mGUIManager.removeComponent("MaxButton");

				/* Two way*/
				   mpCancel->setVisible(false);
				   mpMax->setVisible(false);

				/*Unload mSprite or Resize the mSprite.*/ 
				  mSprite.setAspectRatio(4/-100);
				//mSprite.unload();
				
				
				}
				
			}	

			else if(inpComponent == mpMax)
			{
				
				/* Two way*/
				   mpCancel->setVisible(false);
				   mpMax->setVisible(false);

				/*Unload mSprite or Resize the mSprite.*/ 
				  mSprite.setAspectRatio(4/-100);
				//mSprite.unload();

				frame 	  = new QFrame();
				QPalette* 	palette   = new QPalette();	
				QPixmap*  	pixmap 	  = new QPixmap("C:\\Snowflake SDK\\bin\\applications\\TDigitalSignage\\data\\half_Img\\ad_7.png");
				QBrush* 	brush 	  = new QBrush(*pixmap);
     
				//(Qt::FramelessWindowHint);
	  
				//frame->setFixedSize(1450,700);
				frame->setGeometry(725,30,725,320);
				frame->setFrameStyle(QFrame::Box);
				//frame->setWindowTitle("QFrame Set Background Image Example");   
				//frame->setWindowOpacity(qlevel)
				frame->setWindowFlags(Qt::FramelessWindowHint);
				//frame->setWindowFlags(Qt::WindowCloseButtonHint);
				//frame->setWindowFlags(Qt::Dense1Pattern);
				//frame->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 60);"));
				//tabWidget->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 60);"));
				//Set QFrame Border Width 4 Pixels
				frame->setLineWidth(0);
     

				palette->setBrush(QPalette::Background,*brush);
				frame->setPalette(*palette); 	
      
				frame->show(); 

				mpBigMax->setPosition(QVector2D(0.5f*getWindow().getWindowAspect(),0.57f));
				mpBigMax->setVisible(true);
				

			}

			else if(inpComponent->getName()=="CancelBigButton")
			{
				
				mpBigMax->setVisible(false);
				frame->close();
				
				
			}
			
			
			
		}
		

		
	}

	/**
	* Updates the scaling, position and rotation information of gui elements
	*/
	void BottomInteractable7::repositionGUI()
	{
		
		QVector2D vidPos = mSprite.getPosition(); 
		QVector2D vidSize = QVector2D(mSprite.getAspectRatio(), 1.f)*getScale();
		QVector2D vidBottomLeft = vidPos - vidSize/2.f;
		
		//default menu height
		float menuHeight = 0.1f;
		QVector2D menuButtonSize = QVector2D(1.f, 1.f) * menuHeight * getScale();

		QVector2D mpCancelBigSize = QVector2D(vidSize.y(), vidSize.y())*0.12f;
		mpBigMax->setPosition(QVector2D(1.348f*getWindow().getWindowAspect(),0.98f));
		mpBigMax->setSize(mpCancelBigSize);
		

		//Cancel button settings
		QVector2D cancelPos = vidBottomLeft + QVector2D(menuButtonSize.x()/0.0785f, menuButtonSize.y()/0.1062f);//increase ->lower
		//QVector2D currcancelPos= rotatePointAboutPoint(cancelPos, vidPos, getRotation());
		QVector2D mpCancelSize = QVector2D(vidSize.y(), vidSize.y())*0.12f;
		mpCancel->setPosition(cancelPos);
		mpCancel->setSize(mpCancelSize);
		//mpCancel->setRotation(getRotation());

		//Max button settings
		QVector2D maxPos = vidBottomLeft + QVector2D(menuButtonSize.x()/0.0865f, menuButtonSize.y()/0.1062f);
		//QVector2D currmaxPos= rotatePointAboutPoint(maxPos, vidPos, getRotation());
		QVector2D mpMaxSize = QVector2D(vidSize.y(), vidSize.y())*0.12f;
		mpMax->setPosition(maxPos);
		mpMax->setSize(mpMaxSize);
		//mpMax->setRotation(getRotation());

	}

	/**
	Method for checking if the input is inside the interactable's area.
	\param inBlob the fingerData
	\return bool if the fingerData is inside or not.
	*/
	bool BottomInteractable7::isInside(const TouchListener::TouchData &inBlob)
	{
		QVector2D fingerPos(inBlob.mX, inBlob.mY);
		QVector2D size(mSprite.getWidth(), mSprite.getHeight());
		QVector2D pos = getPosition();
		return Blobz::pointInOrientedRectangle(fingerPos, pos, size, getRotation());
	}

	/**
	* Rotates point A about point B
	* \param inPointA Point A Coordinates
	* \param inPointB Point B Coordinates
	* \param inAngle Rotation Angle in radians
	* \return Rotated point
	*/
	/*QVector2D BottomInteractable7::rotatePointAboutPoint(QVector2D inPointA, QVector2D inPointB, float inAngle )
	{
		QVector2D tPos = inPointA - inPointB;
		float xNew = tPos.x()*cos(inAngle) - tPos.y()*sin(inAngle);
		float yNew = tPos.x()*sin(inAngle) + tPos.y()*cos(inAngle);
		tPos = QVector2D(xNew, yNew);
		tPos = tPos + inPointB;
		return tPos;
	}*/
}